package com.mbc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mbc.dto.StrayAnimalInformationCommentsSectionDTO;
import com.mbc.dto.StrayAnimalInformationDTO;
import com.mbc.mapper.AnimalLabelMapper;
import com.mbc.mapper.StrayAnimalInformationCommentSectionMapper;
import com.mbc.mapper.StrayAnimalInformationMapper;
import com.mbc.service.StrayAnimalInformationService;
import com.mbc.vo.StrayAnimalInformationCommentsSectionVO;
import com.mbc.vo.StrayAnimalInformationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 云助流量动物信息 服务实现类
 * </p>
 *
 * @author suimeng
 * @since 2024-06-06
 */
@Service
public class StrayAnimalInformationServiceImpl implements StrayAnimalInformationService {

    @Autowired
    private StrayAnimalInformationMapper informationMapper;

    @Autowired
    private StrayAnimalInformationCommentSectionMapper sectionMapper;

    @Autowired
    private AnimalLabelMapper animalLabelMapper;

    /**
     * 插入动物信息
     *
     * @param userId         用户 ID
     * @param informationDTO 信息 DTO
     * @return boolean
     */
    @Override
    public synchronized boolean insertAnimalInformation(Integer userId, StrayAnimalInformationDTO informationDTO) {
        /*Integer maxAnimalId = informationMapper.getByMaxAnimalId();
        Integer maxLabelsId = informationMapper.getByMaxLabelsId();
        LocalDateTime now = LocalDateTime.now();
        // 如果数据库为空，设置初始值
        if (maxAnimalId == null && maxLabelsId == null) {
            maxAnimalId = 10000;
            maxLabelsId = 10000;
        } else {
            // 在现有的最大值上分别增加1
            maxAnimalId += 1;
            maxLabelsId += 1;
        }
        // 使用新计算出的ID插入新纪录
        return informationMapper.insertAnimalInformation(userId, maxAnimalId, maxLabelsId, now, informationDTO) > 0;*/
        return informationMapper.insertAnimalInformation(userId, LocalDateTime.now(), informationDTO) > 0;
    }

    /**
     * 新增流浪动物信息评论
     *
     * @param userId     用户 ID
     * @param animalId   动物 ID
     * @param sectionDTO DTO 部分
     * @return boolean
     */
    @Override
    public boolean insertStrayAnimalInformationCommentSection(Integer userId, Integer animalId,
                                                              StrayAnimalInformationCommentsSectionDTO sectionDTO) {
        return sectionMapper.insertStrayAnimalInformationCommentSection(userId, animalId, sectionDTO,
                LocalDateTime.now()) > 0;
    }

    /**
     * 插入动物标签
     *
     * @param labelId 标签 ID
     * @param label   标签
     * @return boolean
     */
    @Override
    public boolean insertAnimalLabel(Integer labelId, String label) {
        return animalLabelMapper.insertAnimalLabel(labelId, label) > 0;
    }

    /**
     * 分页查询流浪动物信息评论区
     *
     * @param animalId 动物 ID
     * @param page     页
     * @param size     大小
     * @return {@link PageInfo }<{@link StrayAnimalInformationCommentsSectionVO }>
     */
    @Override
    public PageInfo<StrayAnimalInformationCommentsSectionVO> selectStrayAnimalInformationCommentSection(Integer animalId, Integer page,
                                                                                                        Integer size) {
        PageHelper.startPage(page, size);
        ArrayList<StrayAnimalInformationCommentsSectionVO> list =
                sectionMapper.selectStrayAnimalInformationCommentSection(animalId);
        return new PageInfo<>(list);
    }

    /**
     * 查询动物信息
     *
     * @param animalId 动物 ID
     * @return {@link StrayAnimalInformationVO }
     */
    @Override
    public StrayAnimalInformationVO selectAnimalInformation(Integer animalId) {
        return informationMapper.selectAnimalInformation(animalId);
    }

    /**
     * 更新点赞数
     *
     * @param animalId 动物 ID
     * @param status   地位
     * @return boolean
     */
    @Override
    public boolean updateNumberOfLikes(Integer animalId,Integer status) {
        if (status == 0){
            return sectionMapper.updateNumberOfLikes1(animalId) > 0;
        }
        return sectionMapper.updateNumberOfLikes2(animalId) > 0;
    }

    /**
     * 删除评论
     *
     * @param animalId 动物 ID
     * @param userId   用户 ID
     * @return boolean
     */
    @Override
    public boolean deleteStrayAnimalInformationCommentSection(Integer animalId, Integer userId) {
        return sectionMapper.deleteStrayAnimalInformationCommentSection(animalId,userId) > 0;
    }

    /**
     * 删除动物信息
     *
     * @param animalId 动物 ID
     * @return boolean
     */
    @Override
    public boolean deleteAnimalInformation(Integer animalId) {
        return informationMapper.deleteAnimalInformation(animalId) > 0;
    }

    /**
     * 更新动物信息
     *
     * @param animalId             动物 ID
     * @param animalInformationDTO 动物信息DTO
     * @return boolean
     */
    @Override
    public boolean updateAnimalInformation(Integer animalId, StrayAnimalInformationDTO animalInformationDTO) {
        return informationMapper.updateAnimalInformation(animalId,animalInformationDTO) > 0;
    }

}
